<?php
namespace app\admin\controller;
class Seller
{
	public function __construct(){
		Admin_auth();
	}
	
    public function index()
    {
		$aid = input('get.aid');
		$phone = input('get.phone');
		if(!empty($aid)){
			$where = ['type'=>1,'status'=>['BETWEEN','1,2'],'aid'=>$aid];
		}else{
			$where = ['type'=>1,'status'=>['BETWEEN','1,2']];
		}
		if(!empty($phone)){
			$where = ['type'=>1,'status'=>['BETWEEN','1,2'],'phone'=>$phone];
		}
		$pages = pages('user',$where,input('get.page'));
		$data = db("user")->where($where)->whereIn('pid',session('wmo2o_a_pid'))->limit($pages['limit'])->order('id desc')->select();
        return view('',['data'  => $data,'pages' => $pages['pages']]);
    }
	
	public function edit()
    {
		$id = input("get.id")?input("get.id"):0;
		$data = array();
		
		if(request()->isPost()){
			$data = input("post.");
			if(db("user")->where(['phone' => $data['phone']])->where('id','<>',$id)->find()){
				return error(lang('tip')['re_phone']);
				exit;
			}
			if($id){
				if(empty($data['password'])){
					unset($data['password']); 
				}else{
					$data['password'] = md5($data['password']);
				}
				db("user")->where(['id' => $id])->update($data);
				$data['id'] = $id;
				$data['password'] = '';
			}else{
				$data['type'] = 1;
				$data['regtime'] = time();
				$data['password'] = md5($data['password']);
				$data['token'] = md5($data['phone'].$data['password']);
				$data['id'] = db("user")->insertGetId($data);
			}
		}else{
			$data = db("user")->where(['type' => '1','id' => $id])->find();
		}
        return view('',['data'  => $data]);
    }
	
	public function del()
    {
		$id = input("get.id")?input("get.id"):0;
		$page = input("get.page");
		$data = db("user")->where(['type' => '1'])->whereIn('pid',session('wmo2o_a_pid'))->delete($id);
		redirect(url('/admin/seller?page='.$page));
		exit;
    }
	
	public function examine()
    {
		$pages = pages('user',['type'=>1,'status'=>3],input('get.page'));
		$data = db("user")->where(['type'=>1,'status'=>3])->limit($pages['limit'])->order('id desc')->select();
        return view('',['data'  => $data,'pages' => $pages['pages']]);
    }
	
	public function setting()
    {
		if(request()->isPost()){
			$data = input("post.");
			db("seller_config")->where(['id'=>1])->update($data);
		}
		$data = db("seller_config")->where(['id'=>1])->find();
        return view('',['data' => $data]);
    }
}
